﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DelegateTest1
{
    public partial class Form1 : Form
    {
        private delegate int Expression(int a, int b);

        private delegate T ExpressionT<T>(T a, T b);

        private delegate void ShowMsgDelegate(string msg);
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Calculate(Add, 10, 11);
            Calculate(Divide, 29, 10);

            Expression add = delegate(int a, int b) { return a + b; };

            Calculate(add, 10, 12);

            Calculate((a, b) => a + b, 10, 12);

            ExpressionT<int> addT = (a, b) => a + b;
            CalculateT(addT, 10, 12);

            Func<int, int, int> addFunc = (a, b) => a + b;
            CalculateFunc(addFunc, 10, 12);
        }


        static int Add(int a, int b)
        {
            return a + b;
        }

        static int Divide(int a, int b)
        {
            return a - b;
        }

        static void Calculate(Expression ex, int a, int b)
        {
            Console.WriteLine(ex(a, b));
        }

        static void CalculateT<T>(ExpressionT<T> ex, T a, T b)
        {
            Console.WriteLine(ex(a, b));
        }

        static void CalculateFunc<T, U, V>(Func<T, U, V> ex, T a, U b)
        {
            Console.WriteLine(ex(a, b));
        }
        private void button2_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(StartImport)).Start();
            //ParameterizedThreadStart start = new ParameterizedThreadStart(StartImport);
            //Thread progressThread = new Thread(start);
            //progressThread.IsBackground = true;
            //progressThread.Start();

        }

        private BaseClass baseClass;
        private void StartImport()
        {
            baseClass = new BaseClass();
            baseClass.StartImport += BaselClass_StartImport;
            baseClass.EndImport += BaseClass_EndImport;
            baseClass.Importing += BaseClass_Importing;
            baseClass.CancelImport += BaseClass_CancelImport;
            baseClass.DoSomeThing();
        }


        private string _msg;
        private void FunShowMsg(string msg)
        {
            this.Invoke(new Action(() => { this.textBox1.AppendText(msg + "\n"); }));
        }

        private void BaseClass_EndImport(object sender, EventArgs e)
        {
            _msg = "导入完成";
            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, _msg);
        }

        private void BaselClass_Importing(int total, int current)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new BaseClass.ImportProgress(BaseClass_Importing), new object[] { total, current });
            }
            else
            {
                progressBar1.Maximum = total;
                progressBar1.Value = current;
            }

        }

        private void BaseClass_Importing(int total, int current)
        {
            this.Invoke(new EventHandler(delegate
            {
                progressBar1.Maximum = (int)total;
                progressBar1.Value = current;
            }));

            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, $"共{total}条数据,正在导入第{current}条数据");
        }

        private void BaselClass_StartImport(object sender, EventArgs e)
        {
            this.Invoke(new EventHandler(delegate { progressBar1.Maximum = (int)sender; }));

            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, "开始任务");
        }

        private void BaseClass_CancelImport(object sender, EventArgs e)
        {
            this.Invoke(new EventHandler(delegate { this.button6.Text = sender.ToString(); }));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            baseClass = new BaseClass();
            baseClass.StartImport += BaselClass_StartImport;
            baseClass.Importing += BaseClass_Importing;
            baseClass.EndImport += BaseClass_EndImport;
            baseClass.CancelImport += BaseClass_CancelImport;
        }




        private void button4_Click(object sender, EventArgs e)
        {
            ExpressionClass.Calculate((a, b) => a + b, 1, 2);
            ExpressionClass.Calculate((a, b) => a + b, 1.5, 2.5);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            baseClass?.DoSomeThing();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            baseClass?.Cancle();
        }


        private void UserControl11_writeLog(object sender, UserControlLogEventArgs e)
        {
            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, e.LogMsg);
        }

        private void userControl11_StartImport(object sender, EventArgs e)
        {
            this.Invoke(new EventHandler(delegate { progressBar1.Maximum = (int)sender; }));

            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, "开始任务");
        }

        private void userControl11_EndImport(object sender, EventArgs e)
        {
            ShowMsgDelegate showMsg = FunShowMsg;
            this.Invoke(showMsg, "任务结束");
        }

    }
}
